package ro.prjmanager.web.actions;

import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.action.ActionBeanContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.impl.LogFactoryImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import ro.prjmanager.core.context.SecurityContext;
import ro.prjmanager.core.service.Service;

@SuppressWarnings({"unchecked"})
public abstract class GeneralAction<M> implements ActionBean {
    private SecurityContext context;
     ro.prjmanager.web.annotation.Service service;
     protected final static Log log = LogFactoryImpl.getLog(ro.prjmanager.web.actions.template.TemplateAction.class);

    protected GeneralAction() {
        service = this.getClass().getAnnotation(ro.prjmanager.web.annotation.Service.class);
    }

    public void setContext(ActionBeanContext actionBeanContext) {
        this.context = (SecurityContext) actionBeanContext;
    }

    public SecurityContext getContext() {
        return this.context;
    }
     public ApplicationContext getSpringContext() {
        return WebApplicationContextUtils.getRequiredWebApplicationContext(context.getServletContext());
    }

    public Service<M, Integer> getService() {
        Service<M, Integer> ret = null;
        if (service != null)
            ret = (Service<M, Integer>) getSpringContext().getBean(service.target());

        return ret;
    }

    public Service<M, Integer> getService(String serviceName) {
        return  (Service<M, Integer>) getSpringContext().getBean(serviceName);
    }
}
